package com.yikaiye.android.yikaiye.ui.activity.login.viewmodel;

import androidx.lifecycle.MutableLiveData;

import com.google.gson.Gson;
import com.yikaiye.android.yikaiye.config.Constant;
import com.yikaiye.android.yikaiye.di.retrofit.BaseModel;
import com.yikaiye.android.yikaiye.di.retrofit.BaseNetCallback;
import com.yikaiye.android.yikaiye.model.UserModel;

import java.util.HashMap;
import java.util.Map;

public class SetPasswordViewModel extends SendCodeViewModel {

    /**
     * 重置密码成功
     */
    public MutableLiveData<UserModel> resetPasswordSuccess = new MutableLiveData<>();
    /**
     * 重置密码失败
     */
    public MutableLiveData<String> resetPasswordError = new MutableLiveData<>();

    /**
     * 密码
     */
    public MutableLiveData<String> password = new MutableLiveData<>();
    /**
     * 再次输入密码
     */
    public MutableLiveData<String> againPassword = new MutableLiveData<>();

    /**
     * 重置密码
     */
    public void resetPassword(String phone, String verifyCode) {
        Map<String, Object> map = new HashMap<>();
        map.put("phone", phone);
        map.put("verifyCode", verifyCode);
        map.put("password", password);
        map.put("conFirmPassword", againPassword);
        map.put("action", 5);

        addSubscribe(mRetrofitHelper.resetPassword(new Gson().toJson(map)).subscribeWith(new BaseNetCallback<UserModel>() {

            @Override
            public void onSuccess(BaseModel<UserModel> baseModel) {
                if (Constant.SUCCESS == baseModel.getCode()) {
                    if (null != baseModel.getData()) {
                        resetPasswordSuccess.setValue(baseModel.getData());
                    }
                } else {
                    resetPasswordError.setValue(baseModel.getMsg());
                }
            }

            @Override
            public void onError(String message) {
                super.onError(message);
                resetPasswordError.setValue(message);
            }
        }));
    }
}
